package com.demo;

public class Demo02 {


    public static String parseToMoney(double number) {
        // 将数字转为字符串，处理成整数部分 + 小数部分
        String numStr = Double.toString(number);
        String[] parts = numStr.split("\\.");
        String integerPart = parts[0];
        String decimalPart = parts.length > 1 ? "." + parts[1] : "";

        // 对整数部分添加千分位逗号
        StringBuilder formattedInteger = new StringBuilder();
        int length = integerPart.length();

        for (int i = 0; i < length; i++) {
            // 够三位加一个，
            if (i > 0 && (length - i) % 3 == 0) {
                formattedInteger.append(",");
            }
            formattedInteger.append(integerPart.charAt(i));
        }

        return formattedInteger.toString() + decimalPart;
    }


    public static void main(String[] args) {
        System.out.println(parseToMoney(1234.56));
        System.out.println(parseToMoney(123456789));
        System.out.println(parseToMoney(1087654.321));
        System.out.println(parseToMoney(98765));
        System.out.println(parseToMoney(7654321.9));
    }

}
